<template>
  <div class="uploadBook">
    <el-upload
      class="upload-container"
      action="http://localhost:3000/book/upload"
      :headers="headers"
      :on-success="onSuccess"
      :on-remove="onRemove"
      :on-exceed="onExceed"
      :disabled="isEdit"
      :limit="limit"
      :file-list="fileList"
      drag
      multiple
    >
      <i class="el-icon-upload" />
      <div>
        将文件拖到这里，或<em>点击上传</em>
      </div>
    </el-upload>
  </div>
</template>

<script>
import { getToken } from './../../utils/auth'
export default {
  name: 'UploadBook',
  props: {
    isEdit: {
      default: false,
      type: Boolean
    },
    fileList: {
      type: Array
    }
  },
  data() {
    return {
      limit: 1
    }
  },
  computed: {
    headers() {
      return {
        'authorization': `Bearer ${getToken()}`
      }
    }
  },
  methods: {
    onSuccess(res, file, fileList) {
      this.$message({
        type: 'success',
        message: '上传成功'
      })
      console.log(fileList)
      if (res.data) {
        this.$emit('onSuccess', res.data)
      }
    },
    onRemove(file, fileList) {
      this.$emit('onRemove', file.response)
    },
    onExceed(file, fileList) {
      this.$message({
        type: 'warning',
        message: '一次只能上传一个epub文件'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.uploadBook{
  .upload-container{
    width: 100%;
    em{
      color: #0a76a4;
    }
  }
}
</style>
